﻿using IMES.Common.Domain.DariusModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using Mysqlx.Crud;

namespace Darius.API.Application.Commands.Approval
{
    public class ApprovalSearchHandler : IRequestHandler<ApprovalSearchCommand, Result>
    {
        private readonly IBaseRepositorys<Approvals> approvalrepository;//审批表
        private readonly IBaseRepositorys<ApprovalApplications> approvalapplications;//审批流程表
        private readonly IBaseRepositorys<ApprovalProcess> approvalprocess;//审批申请表

        public ApprovalSearchHandler(IBaseRepositorys<Approvals> approvalrepository, IBaseRepositorys<ApprovalApplications> approvalapplications, IBaseRepositorys<ApprovalProcess> approvalprocess)
        {
            this.approvalrepository = approvalrepository;
            this.approvalapplications = approvalapplications;
            this.approvalprocess = approvalprocess;
        }
        //查看审核列表
        public async Task<Result> Handle(ApprovalSearchCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CodeEnum.search};
            var list=(from a in approvalapplications.GetAll()
                     join b in approvalprocess.GetAll() on a.SubmitEventId equals b.ApprovalProcessId
                      where a.ApprovalStatus == 0
                      select new ApprovalApplicationDto
                     {
                         ApprovalProcessId=b.ApprovalProcessId,
                         ApprovalProcessName=b.ApprovalProcessName,
                         ApprovalProcessLevel=b.ApprovalProcessLevel,
                         Approvalers = b.Approvalers,
                         ApprovalApplicationId=a.ApprovalApplicationId,
                         SubmitlDate=a.SubmitlDate,
                         SubmitName = a.SubmitName,
                         SubmitEventId=a.SubmitEventId,
                         OrderId=a.OrderId,
                         ApprovalStatus = a.ApprovalStatus,
                         ApprovalListDtos =new List<ApprovalListDto>()
                     }).ToList();
            foreach (var item in list)
            {
                item.ApprovalListDtos=(from a in approvalrepository.GetAll()
                                      join b in approvalapplications.GetAll() on a.ApprovalApplicationId equals b.ApprovalApplicationId
                                      where (a.Approvaler==request.username&&b.ApprovalApplicationId == item.ApprovalApplicationId)
                                      select new ApprovalListDto
                                      {
                                          ApprovalApplicationId = a.ApprovalApplicationId,
                                   
                                          Approvaler = a.Approvaler,
                                          ApprovalTime=a.ApprovalTime,
                                      }).ToList();
            }
            result.Data = list;
            return result;
        }
    }
}
